import Vue from 'vue'
import VueRouter from 'vue-router'
import { isPC } from '@/plugins/util'

import { mainChildrens } from './mainRouter'
import { userChildrens } from './userRouter'
import hostChildrens from './hostRouter'
import onlyview from '@/layouts/onlyview.js'

const routes = [
  {
    path: '/',
    name:'root',
    redirect: '/SRC/competition',
  },
  {
    path:'/404NotFound/:fullPath',
    name:'404',
    component:() => import('@/views/404/index'),
    meta:{
      title:'页面丢失了'
    },
    props:true
  },
  {  
    path:'/user',
    name:'user',
    redirect: '/user/relog',
    component:() => import('@/views/User/index.js'),
    children:userChildrens,
  },
  {
    path:'/SRC',
    name:'main',
    redirect: '/SRC/competition',
    component:() => import('@/views/Main/index'),
    children:mainChildrens
  },
  {
    path:'/hostpage',
    name:'hostpage',
    redirect: '/hostpage/htindex',
    component:() => import('@/views/HostPage/index'),
    children:hostChildrens
  }
  /* {
    path: '/PC',
    name: 'pc',
    component:() => import('@/views/Main/PC.vue'),
    children:PCUserRouter
  },
  {
    path: '/mobile',
    name: 'mobile',
    component:() => import('@/views/Main/H5.vue'),
    children:H5UserRouter
  },
  { 
    path:'/404',
    name:'404',
    component:() => import('@/views/404/index.vue'),
  },
  {
    path:'/test',
    name: 'test',
    component:() => import('@/api/ApiTest.vue')
  } */
]

const router = new VueRouter({
  mode:'history',
  routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

import { Toast } from 'vant'
import store from '@/store/index'
import { message } from 'element-ui'
router.beforeEach((to,from,next)=>{
  const { name,matched:[{name:rootName}] } = to
  console.log({to,rootName})
  if (name === null){
    let fullPath = from.fullPath
    if (fullPath === '/') fullPath = false
    next(`/404NotFound/${fullPath}`)
  }
  console.log(from.name,"=>",name)
  const { token,info:{type} } = store.getters
  if((!token || !type) && (
    rootName=='hostpage' || false))
  {
    message.error({
      message:'用户权限不足',
      showClose:true
    })
    next('/')
  }
  Toast.clear(true)
  next()
})

router.afterEach((to)=>{
  let { title } = to.meta
  title = `StartRiverCup${title? `-${title}`:''}`
  document.title = title
})

export default router
